package com.systemframework.oss;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectResult;
import com.systemframework.constants.Constants;
import com.systemframework.exception.ResultException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * @author summer
 * @date 2024-06-17 19:59
 */
@Configuration
@ConfigurationProperties(prefix = "oss")
@Data
@Slf4j
public class OssConfigure {

    private String endpoint;

    private String accessKeyId;

    private String accessKeySecret;

    private String bucketName;

    private String accessUrl;

    private String tempFile;



    public void ossClient(MultipartFile file) throws  IOException {
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);
        oss.putObject(bucketName,  file.getOriginalFilename(), file.getInputStream());
        oss.shutdown();
    }

    public String uploadFile(MultipartFile file) {
        try {
            if(StrUtil.isEmpty(file.getOriginalFilename())){
              throw new ResultException("请上传文件");
            }
            String format = DateUtil.format(new Date(), Constants.DATE_TIME_FORMAT)+Constants.SLASH+file.getOriginalFilename();
            OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);
            oss.putObject(bucketName,  format,file.getInputStream());
            return format;
        }catch (Exception e){
            log.error("上传文件失败:{}",e.getMessage());
            throw new ResultException("上传文件失败");
        }

    }

    public void remove(String fileName){
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);
        oss.deleteObject(bucketName, fileName);
    }

    public String uploadFile(InputStream inputStream) {
       String format = DateUtil.format(new Date(), Constants.DATE_TIME_FORMAT)+Constants.SLASH+ IdUtil.fastSimpleUUID()+Constants.IMAGE_TYPE_PNG;
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);
        oss.putObject(bucketName,  format,inputStream);
        return format;
    }

    public String uploadFile(File file){
        String format = DateUtil.format(new Date(), Constants.DATE_TIME_FORMAT)+Constants.SLASH+file.getName();
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);
        PutObjectResult putObjectResult = oss.putObject(bucketName,  format, file);
        if(putObjectResult.getResponse()!=null){
            log.error("上传文件失败:{}",putObjectResult.getResponse().getErrorResponseAsString());
            throw new ResultException("上传文件失败");
        }
        return format;
    }


    public InputStream downloadFile(String fileName) {
        OSS oss = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);
        OSSObject object = oss.getObject(bucketName,  fileName);
        return object.getObjectContent();
    }

}
